package com.soft.mainsite.component.beetl.tag.site;

import com.soft.mainsite.common.exception.CmsException;
import com.soft.mainsite.common.utils.Pojo2MapUtil;
import com.soft.mainsite.module.web.cms.service.SiteService;
import com.soft.mainsite.module.web.cms.vo.TCmsSiteVo;
import org.beetl.core.GeneralVarTagBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Description:查询站点信息
 *
 * @author liupengtao
 * @create 2019/05/05
 **/
@Service
@Scope("prototype")
public class SiteInfoTag extends GeneralVarTagBinding {

    @Autowired
    private SiteService siteService;

    @Value("${system.site.subfix}")
    private String siteSubfix;

    @Value("${system.site.prefix}")
    private String sitePrefix;

    @Override
    public void render() {
        Integer siteId = (this.getAttributeValue("siteId") instanceof String) ? Integer.parseInt((String) this.getAttributeValue("siteId")) : (Integer) this.getAttributeValue("siteId");
        if (siteId == null) {
            throw new CmsException("查询站点信息时siteId不能为空！！");
        }
        try {
            wrapRender(siteId);
        } catch (Exception e) {
            throw new CmsException("SiteinfoTag wrapRender异常==" + e.getMessage());
        }

    }

    private void wrapRender(Integer siteId) throws Exception {
        TCmsSiteVo voById = siteService.findVoById(siteId);
        Map result = Pojo2MapUtil.toMap(voById);
        this.binds(result);
        this.doBodyRender();
    }
}
